package com.apps.express.Express100.model.po;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apps.sannotation.constraints.NoNull;
import com.apps.saspect.ValidMethod;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 快递数据模型
 */
@ApiModel("订单的快递信息模型")
@Data
public class Express {
    @ApiModelProperty("快递公司名称")
    private String company_name="";
    @ApiModelProperty("快递公司编号")
    private String company_code="";
    @ApiModelProperty("快递单号")
    private String ship_number="";
    @ApiModelProperty("是否已签收")
    private Integer isComplete=0;
    @ApiModelProperty("快递单当前状态，包括0在途，1揽收，2疑难，3签收，4退签，5派件，6退回，7转投，10待清关，11清关中，12已清关，13清关异常，14拒签 等13个状态")
    private Integer state=0;
    @ApiModelProperty("状态的中文名称")
    private String state_name="";
    @ApiModelProperty("数组，包含多个对象，每个对象字段如展开所示")
    private List<ExpressItem> data=new ArrayList<>();
    @ApiModelProperty("快递路由信息,这个字段有可能为NULL,前端需要自行处理")
    private ExpressRoute expressRoute;

    @ValidMethod
    public void setCompany_code(@NoNull String company) {
        this.company_code = company;
    }
    @ValidMethod
    public void setShip_number(@NoNull String ship_number) {
        this.ship_number = ship_number;
    }
    @ValidMethod
    public void setIsComplete(@NoNull Integer isComplete) {
        this.isComplete = isComplete;
    }
    @ValidMethod
    public void setState(@NoNull Integer state) {
        this.state = state;
    }
    @ValidMethod
    public void setData(@NoNull List<ExpressItem> data) {
        this.data = data;
    }

    @ValidMethod
    public void setCompany_name(@NoNull String company_name) {
        this.company_name = company_name;
    }
    @ValidMethod
    public void setExpressRoute(@NoNull ExpressRoute expressRoute) {
        this.expressRoute = expressRoute;
    }
    @ValidMethod
    public void setExpressRoute(@NoNull JSONObject expressRoute) {
        this.expressRoute = null != expressRoute ? conversionExpressRouteInfo(expressRoute):null;
    }

//    /**
//     * 将数据库的EsOrderExpress 映射成快递模型
//     * @param orderExpress
//     */
//    public Express(EsOrderExpress orderExpress) {
//        this.company_name = orderExpress.getCompany_name();
//        this.company_code = orderExpress.getCompany();
//        this.isComplete=orderExpress.getIs_complete();
//        this.state = orderExpress.getState();
//        this.state_name = ExpressStatus.getCodeByKudi100(orderExpress.getState()).getKuaidi100();
//        this.ship_number = orderExpress.getShip_number();
//        this.data = conversionExpressItemKudi100(JSONArray.parseArray(orderExpress.getData()));
//        this.expressRoute = StringUtil.isNotEmpty(orderExpress.getRoute_info())?conversionExpressRouteInfo(JSONObject.parseObject(orderExpress.getRoute_info())):null;
//    }

    public Express()
    {

    }

    /**
     * 将数据库存储的字符串快递明细转成明细模型数组
     * @param jsonArray
     * @return
     */
    @ValidMethod
    protected List<ExpressItem> conversionExpressItemKudi100(@NoNull JSONArray jsonArray)
    {
        List<ExpressItem> expressItems=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++)
        {
            ExpressItem item=new ExpressItem();
            item.setContent(jsonArray.getJSONObject(i).getString("content"));
            item.setGmt_create(jsonArray.getJSONObject(i).getTimestamp("gmt_create").toLocalDateTime());
            item.setStatus(jsonArray.getJSONObject(i).getInteger("status"));
            expressItems.add(item);
        }
        return expressItems;
    }

    /**
     * 将字符串的快递路由信息转成实体对象
     * @param jsonObject
     * @return
     */
    @ValidMethod
    protected ExpressRoute conversionExpressRouteInfo(@NoNull JSONObject jsonObject)
    {
        ExpressRoute expressRoute=new ExpressRoute();
        try {
            expressRoute.setFrom(new ExpressRouteItem(jsonObject.getJSONObject("from").getString("number"),jsonObject.getJSONObject("from").getString("name")));
            expressRoute.setCur(new ExpressRouteItem(jsonObject.getJSONObject("cur").getString("number"),jsonObject.getJSONObject("cur").getString("name")));
            expressRoute.setTo(new ExpressRouteItem(jsonObject.getJSONObject("to").getString("number"),jsonObject.getJSONObject("to").getString("name")));
        }catch (Exception e)
        {
            return null;
        }

        return expressRoute;
    }
}
